<template>
  <div class="image-upload-container">
    <el-upload
      :class="{ disabled: noUpload }"
      :auto-upload="false"
      list-type="picture-card"
      :on-change="checkImageFormat"
      :on-remove="handleRemove"
      :limit="limit"
      :file-list="faceList"
      accept=".jpg,.png,.jpeg"
    >
      <el-icon v-if="faceList.length < limit">
        <Plus />
      </el-icon>
    </el-upload>
  </div>
</template>

<script lang="ts">
import { defineComponent, onMounted, ref, watch } from "vue";
import { ElUpload, UploadFile, UploadProps } from "element-plus";
import { Plus } from "@element-plus/icons-vue";
import { uploadFileApi } from "@/api/common";

export default defineComponent({
  name: "ImageUpload",
  components: {
    Plus,
    ElUpload
  },
  props: {
    headImageList: {
      type: Array,
      required: true,
      default: []
    },
  },
  emits: ["uploadFileChange", "fileRemoved", "fileAdded"], // 定义自定义事件
  setup(props, { emit }) {
    const faceList = ref([]); //图片列表
    const noUpload = ref(false); //不再上传
    const disabled = ref(false);
    const limit = ref(99);
    const fileIndex = ref(0);

    const handleRemove: UploadProps["onRemove"] = (uploadFile, uploadFiles) => {
      faceList.value = uploadFiles;
      // 查找元素的索引
      emit("fileRemoved", uploadFile);
      if (faceList.value.length < limit.value) {
        noUpload.value = false;
      }
    };

    const checkImageFormat = file => {
      if (faceList.value.length === limit.value - 1) {
        noUpload.value = true; //设置为true阻止继续上传
      }
      // 创建一个新的 FormData 对象并添加文件
      const formData = new FormData();
      formData.append("file", file.raw);
      // 验证 FormData 的内容
      uploadFileApi(formData)
        .then(res => {
          if (res.code === "0000") {
            faceList.value.push({
              name: file.raw.name.split(".")[0],
              url: res.data,
              uid: file.raw.uid
            });
          }
          fileIndex.value = faceList.value.length - 1;
          const data = {
            index: fileIndex.value,
            name: file.raw.name.split(".")[0]
          };
          emit("fileAdded", data);
        })
        .catch(err => {
          console.log("上传banner失败::::" + err);
        });
      return true; // 允许文件上传
    };
    onMounted(() => {
      faceList.value = props.headImageList;
      if (faceList.value.length > 0) {
        faceList.value.forEach((item, index) => {
          item.url = item.image; // 使用原始的 image 值作为 url
          item.index = index; // 添加索引值
        });
      }
      if (faceList.value.length === limit.value) {
        noUpload.value = true;
      }
    });
    watch(
      () => faceList.value,
      newVal => {
        faceList.value = newVal;
      },
      { deep: true }
    );
    return {
      handleRemove,
      faceList,
      disabled,
      noUpload,
      checkImageFormat,
      limit
    };
  }
});
</script>

<style scoped>
:deep(.el-upload-list .el-upload-list--picture-card){
  width: 500px;
}
:deep(.el-upload--picture-card) {
  width: 50px;
  height: 50px;
}
:deep(.disabled .el-upload--picture-card) {
  display: none !important;
}

/* 隐藏el-upload组件的放大镜图标 */
::v-deep .el-upload-list__item-preview .el-icon--zoom-in {
  display: none !important;
}

::v-deep .el-upload-list__item-delete .el-icon--delete {
  margin-right: 15px;
}

.image-upload-container {
  display: flex;
  flex-wrap: wrap;
  gap: 50px;
  width: 350px; /* 4个50px + 3个10px的间隔 */
  height: 100%;
}

.upload-box {
  width: 50px;
  height: 50px;
}

.uploaded-image {
  width: 100%;
  height: 100%;
}

.upload-icon {
  font-size: 24px;
  line-height: 50px;
}
</style>
